home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et-2_2.lha / et2.2 / src / SUNOS / sunsystem.c < prev    next >
C/C++ Source or Header  |  1990-12-05  |  7KB  |  389 lines

  1. #include <sys/types.h>
  2.  
  3. #ifdef sun
  4. #include <sgtty.h>
  5. #include <dirent.h>
  6. #else
  7. #include <sys/dir.h>
  8. #endif
  9.  
  10. #include <sys/stat.h>
  11. #include <setjmp.h>
  12. #include <signal.h>
  13. #include <stdio.h>
  14. #include <sys/param.h>
  15. #include <pwd.h>
  16. #include <errno.h>
  17. #include <sys/wait.h>
  18. #include <sys/time.h>
  19. #include <sys/file.h>
  20.  
  21. typedef int bool;
  22. #define TRUE 1
  23. #define FALSE 0
  24.  
  25. extern int errno;
  26. extern char *sys_errlist[];
  27. extern char *sys_siglist[];
  28.  
  29. extern char *malloc();
  30. extern void free();
  31.  
  32. typedef void (*sigHandlerType)();
  33.  
  34. int sunwindow_select(nfds, readready, writeready, timeout)
  35. unsigned int nfds, *readready, *writeready, timeout;
  36. {
  37.     extern int select();
  38.     
  39.     int retcode;
  40.     struct timeval tv;
  41.     
  42.     tv.tv_sec= timeout / 1000;
  43.     tv.tv_usec= (timeout % 1000) * 1000;
  44.     retcode= select(nfds, readready, writeready, 0, &tv);
  45.     if (retcode == -1) {
  46.     if (errno == EINTR)
  47.         return -2;
  48.     return -1;
  49.     }
  50.     return retcode;
  51. }
  52.  
  53. #define MAXSIGNALS 11
  54.  
  55. struct signal_map {
  56.     int code;
  57.     int (*handler)();
  58. } signal_map[MAXSIGNALS]= {
  59.     SIGBUS, 0, 
  60.     SIGSEGV, 0, 
  61.     SIGSYS, 0, 
  62.     SIGPIPE, 0, 
  63.     SIGILL, 0, 
  64.     SIGQUIT, 0, 
  65.     SIGINT, 0, 
  66.     SIGWINCH, 0, 
  67.     SIGALRM, 0, 
  68.     SIGCHLD, 0, 
  69.     SIGURG, 0
  70. };
  71.  
  72. static void sighandler(sig)
  73. int sig;
  74. {
  75.     int i;
  76.     
  77.     for (i= 0; i < MAXSIGNALS; i++)
  78.     if (signal_map[i].code == sig) {
  79.         (*signal_map[i].handler)(i);
  80.         return;
  81.     }
  82. }
  83.  
  84. void sunsystem_signal(sig, handler)
  85. int sig;
  86. int (*handler)();
  87. {
  88.     struct signal_map *sm= &signal_map[sig];
  89.     sm->handler= handler;
  90.     signal(sm->code, sighandler);
  91. }
  92.  
  93. void sunsystem_resetsignal(sig)
  94. int sig;
  95. {
  96.     struct signal_map *sm= &signal_map[sig];
  97.     signal(sm->code, SIG_DFL);
  98. }
  99.  
  100. char *sunsystem_signame(sig)
  101. int sig;
  102. {
  103.     return sys_siglist[signal_map[sig].code];    
  104. }
  105.  
  106. char *workingdirectory()
  107. {
  108.     static char path[MAXPATHLEN];
  109.  
  110.     if (getwd(path) == 0) {
  111.     strcpy(path, "/"); 
  112.     Warning("workingdirectory", "getwd failed");
  113.     }
  114.     return path;
  115. }
  116.  
  117. char *homedirectory()
  118. {
  119.     static char path[MAXPATHLEN];
  120.     struct passwd   *pw;
  121.  
  122.     if ((pw= getpwuid(getuid())) == 0) 
  123.     return 0;
  124.     strncpy(path,pw->pw_dir,MAXPATHLEN);
  125.     return path;
  126. }
  127.  
  128. char *getdirentry(dirp)
  129. DIR *dirp;
  130. {
  131. #ifdef sun
  132.     struct dirent *dp;
  133. #else
  134.     struct direct *dp;
  135. #endif sun
  136.      
  137.     if (dirp && (dp= readdir(dirp)))
  138.     return dp->d_name;
  139.     return 0;
  140. }
  141.  
  142. void *getstatbuf(path)
  143. char *path;
  144. {
  145.     struct stat *statbuf;
  146.     
  147.     if (path != 0) {
  148.     statbuf= (struct stat*) malloc(sizeof (struct stat));
  149.     if (statbuf == NULL)
  150.         return NULL;
  151.     if (stat(path, statbuf) < 0)
  152.         return NULL;
  153.     return (void*) statbuf;
  154.     }
  155.     return NULL;
  156. }
  157.  
  158. void freestatbuf(statbuf)
  159. struct stat *statbuf;
  160. {
  161.     if (statbuf)
  162.     free(statbuf);
  163. }
  164.  
  165. int uniquefileid(statbuf)
  166. struct stat *statbuf;
  167. {    
  168.     return statbuf->st_dev + statbuf->st_ino;
  169. }
  170.  
  171. int isexecutable(statbuf)
  172. struct stat *statbuf;
  173. {
  174.     if (statbuf->st_mode & ((S_IEXEC)|(S_IEXEC>>3)|(S_IEXEC>>6)))
  175.     return 1;
  176.     return 0;
  177. }
  178.  
  179. int isdirectory(statbuf)
  180. struct stat *statbuf;
  181. {
  182.     if ((statbuf->st_mode & S_IFMT) == S_IFDIR ) 
  183.     return 1;
  184.     return 0;
  185. }
  186.  
  187. int isspecial(statbuf)
  188. struct stat *statbuf;
  189. {
  190.     if ((statbuf->st_mode & S_IFMT) != S_IFREG && !isdirectory(statbuf))
  191.     return 1;
  192.     return 0;
  193. }
  194.  
  195. long filesize(statbuf)
  196. struct stat *statbuf;
  197. {
  198.     return (long) statbuf->st_size;   
  199. }
  200.  
  201. int sunsystem_GetTtyChars(backspace, rubout)
  202. char *backspace, *rubout;
  203. {
  204.     struct sgttyb ttyBuf;
  205.     struct tchars tcharBuf;
  206.     struct ltchars ltcharBuf;
  207.     int fd;
  208.  
  209.     if ((fd= open("/dev/tty", O_RDONLY)) < 0) {
  210.     fprintf(stderr, "GetTtyChars: can't open control terminal");
  211.     return 1;
  212.     }
  213.  
  214.     if (ioctl(fd, (int) TIOCGETP, (char*) &ttyBuf) == -1 ||
  215.         ioctl(fd, (int) TIOCGETC, (char*) &tcharBuf) == -1 ||
  216.         ioctl(fd, (int) TIOCGLTC, (char*) <charBuf) == -1) {
  217.     close(fd);
  218.     return 1;
  219.     }    
  220.     close(fd);
  221.  
  222.     *backspace= ttyBuf.sg_erase;
  223.     *rubout= tcharBuf.t_intrc;
  224.     return 0;
  225. }
  226.  
  227. static jmp_buf buf;
  228.  
  229. void sunsystem_LongJmp()
  230. {
  231.     longjmp(buf, 1);
  232. }
  233.  
  234. typedef void (*CallFunc)();
  235. static int retcode;
  236.  
  237. int sunsystem_Call(cf, p1, p2, p3, p4)
  238. CallFunc cf;
  239. char *p1, *p2, *p3, *p4;
  240. {
  241.     retcode= 0;
  242.     
  243.     if (setjmp(buf))
  244.     retcode= 1;
  245.     else
  246.     (*cf)(p1, p2, p3, p4);
  247.     /*if (retcode)
  248.     fprintf(stderr, "sunsystem_Call: %d\n", retcode);*/
  249.     return retcode;
  250. }
  251.  
  252. /* expand the metacharacters as in the shell */
  253.  
  254. static char *shellMeta   = "~*[]{}?$",
  255.         *shellStuff  = "(){}<>\"'",
  256.         shellEscape = '\\';
  257.  
  258. char *expandpathname(patbuf, buflen)
  259. char *patbuf;
  260. int buflen;
  261. {
  262.     char cmd[170], StuffedPat[200], name[70];
  263.     register char *p, *q;
  264.     FILE *pf;
  265.     struct passwd *pw;
  266.     int ch, i;
  267.  
  268.     /* skip leading blanks */
  269.     while (*patbuf == ' ')
  270.     patbuf++;
  271.  
  272.     /* any shell meta characters ? */
  273.     for (p= patbuf; *p; p++)
  274.     if (index(shellMeta, *p))
  275.         goto needshell;
  276.  
  277.     return 0;
  278.  
  279. needshell:    
  280.     /* escape shell quote characters */
  281.     StuffChar(patbuf, StuffedPat, sizeof StuffedPat, shellStuff, shellEscape);
  282.  
  283.     strcpy(cmd, "echo -n ") ;
  284.     strcat(cmd, " ");
  285.  
  286.     /* emulate csh -> popen executes sh */
  287.     if (StuffedPat[0] == '~') {
  288.     if (StuffedPat[1] != '\0' && StuffedPat[1] != '/') {
  289.         /* extract user name */
  290.         for (p= &StuffedPat[1], q= name; *p && *p !='/';)
  291.         *q++= *p++;
  292.         *q = '\0';
  293.         if ((pw= getpwnam(name)) == NULL) 
  294.         strcat(cmd, StuffedPat); 
  295.         else {
  296.         strcat(cmd, pw->pw_dir);            
  297.         strcat(cmd, p);
  298.         }           
  299.     } else {
  300.         if ((pw= getpwuid(getuid())) == NULL)
  301.         return sys_errlist[errno];
  302.         strcat(cmd, pw->pw_dir);            
  303.         strcat(cmd, &StuffedPat[1]);
  304.     }
  305.     } else
  306.     strcat(cmd, StuffedPat);
  307.  
  308.     if ((pf= popen(&cmd[0], "r")) == NULL)
  309.     return sys_errlist[errno];
  310.  
  311.     /* read first argument */
  312.     for (i= 0, ch= getc(pf); ch != EOF && ch != ' '; ch= getc(pf)) {
  313.     if (i == buflen-1)
  314.         break;
  315.     patbuf[i++]= ch;
  316.     }
  317.     patbuf[i]= '\0';
  318.  
  319.     /* overread rest of pipe */
  320.     while (ch != EOF) {
  321.     ch= getc(pf);
  322.     if (ch == ' ' || ch == '\t')
  323.         return "expression ambigous";
  324.     }
  325.  
  326.     pclose(pf);
  327.  
  328.     return 0;
  329. }
  330.  
  331. int waitchild()
  332. {
  333.     union wait status;
  334.     
  335.     return wait3(&status, WNOHANG, 0);
  336. }
  337.  
  338. int myread(ifp, millisec, b, size)
  339. FILE *ifp;
  340. int millisec, size;
  341. void *b;
  342. {
  343.     int rval, fd;
  344.     fd_set readready;
  345.     struct timeval t, *tv= &t;
  346.     
  347.     fd= fileno(ifp);
  348.            
  349.     if (millisec < 0) /* block */
  350.     tv= 0;
  351.     else if (millisec == 0) /* poll */
  352.     t.tv_sec= t.tv_usec= 0L;
  353.     else {
  354.     t.tv_sec= (long) (millisec / 1000);
  355.     t.tv_usec= (long) ((millisec % 1000) * 1000);
  356.     }
  357.     for (;;) {
  358.     FD_ZERO(&readready);
  359.     FD_SET(fd, &readready);
  360.     if ((rval= select(fd+1, &readready, 0, 0, tv)) < 0) {
  361.         if (errno != EINTR)
  362.         perror("select");
  363.         continue;
  364.     }
  365.     if (rval == 0) /* timeout */
  366.         return 1;
  367.     if (FD_ISSET(fd, &readready)) {
  368.         rval= fread(b, size, 1, ifp);
  369.         if (rval == 0) {
  370.         if (millisec < 0)
  371.             Error("myread", "server died");
  372.         return 1;
  373.         }
  374.         return 0;
  375.     }
  376.     }
  377. }
  378.  
  379. #ifdef sony
  380.  
  381. char *tempnam(dir, pfx)
  382. char *dir, *pfx;
  383. {
  384.     return "/tmp/et";
  385. }
  386.  
  387. #endif sony
  388.  
  389.